package com.fary.api;

import cn.hutool.core.util.StrUtil;
import com.fary.common.ApiResponse;
import com.fary.dto.PayMerchantQrCodeRespDTO;
import com.fary.service.PayMerchantQrCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/merchant")
@Api(tags = "二维码接口")
public class PayMerchantQrCodeApi {

    @Autowired
    private PayMerchantQrCodeService payMerchantQrCodeService;

    /**
     * 生成临时二维码
     */
    @GetMapping("generateQrCode")
    @ApiOperation(value = "二维码接口", notes = "生成临时二维码")
    public ApiResponse<PayMerchantQrCodeRespDTO> generateQrCode() {
        PayMerchantQrCodeRespDTO payMerchantQrCodeRespDTO = payMerchantQrCodeService.generateQrCode();
        if (payMerchantQrCodeRespDTO == null) {
            log.error("临时二维码生成失败");
            return ApiResponse.error("生成二维码失败!");
        }
        return ApiResponse.success(payMerchantQrCodeRespDTO);
    }

    @GetMapping("getQrCodeTokenResult")
    @ApiOperation(value = "二维码扫码查询", notes = "根据token查询是否扫码过")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "qrCodeToken", value = "查询二维码token", required = true)
    })
    public ApiResponse<String> getQrCodeTokenResult(String qrCodeToken) {
        if (StrUtil.isEmpty(qrCodeToken)) {
            return ApiResponse.error("qrCodeToken is null");
        }
        boolean qrCodeTokenResult = payMerchantQrCodeService.getQrCodeTokenResult(qrCodeToken);
        return qrCodeTokenResult ? ApiResponse.success("提交成功") : ApiResponse.error("提交失败");
    }
}
